<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">注册小正博客账户</div>
            <el-form :model="param" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="用户名">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="密码" v-model="param.password">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="emailbox">
                    <el-input type="text" placeholder="邮箱" v-model="param.emailbox">
                        <el-button slot="prepend" icon="iconfont icon-youxiang"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="verif">
                    <el-input type="text" placeholder="验证码" v-model="param.verif">
                        <el-button slot="prepend" icon="iconfont icon-yanzhengma"></el-button>
                    </el-input>
                </el-form-item>
                <el-image style="width: 200px; height: 50px; margin: 3px" :src="verUrl" fit="contain" @click="refVerificationCode">
                </el-image>
                <div class="register-sendM-btn">
                    <el-button type="info" @click="register_send_fun()">发送邮箱验证码</el-button>
                </div>
                <el-form-item prop="verifmail">
                    <el-input type="text" placeholder="邮箱中验证码" v-model="param.verifmail">
                        <el-button slot="prepend" icon="iconfont icon-yanzhengma"></el-button>
                    </el-input>
                </el-form-item>
                <div class="register-btn">
                    <el-button type="success" @click="register_fun()">注册</el-button>
                </div>
                <div class="login-btn">
                    <el-button type="primary" @click="toLogin()">已有账号,前往登录</el-button>
                </div>
                <p class="login-tips">Tips : 小正科技版权所有</p>
            </el-form>
        </div>
    </div>
</template>

<script>
import { sendEMail, register } from '@/api/UserApi';
import { getApiBase } from '@/utils/request';
export default {
    data: function () {
        return {
            timeM: '',
            verUrl: getApiBase() + '/web/open/GenerateVerificationCode?date=' + this.getTime(),
            param: {
                username: '',
                password: '',
                emailbox: '',
                verif: '',
                verifmail: ''
            }
        };
    },
    methods: {
        async register_send_fun() {
            const res = await sendEMail(this.param.username, this.param.emailbox, this.timeM, this.param.verif);
            if (res === null) return;
            this.$message.success(res.msg);
        },
        async register_fun() {
            const res = await register(this.param.username, this.param.emailbox, this.param.password, this.param.verifmail);
            if (res === null) return;
            this.$message.success(res.msg);
        },
        toLogin() {
            this.$router.push('/login');
        },
        refVerificationCode() {
            this.verUrl = getApiBase() + '/web/open/GenerateVerificationCode?date=' + this.getTime();
        },
        getTime() {
            this.timeM = Date.now();
            return this.timeM;
        }
    },
    created(){
        this.refVerificationCode()
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: rgb(245, 58, 235);
    border-bottom: 1px solid rgb(241, 51, 178);
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.register-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.register-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: rgb(238, 39, 115);
}
.register-sendM-btn {
    margin-block: 10px;
    width: 50px;
}
</style>